package com.dlc.felear.xiaoensale.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.dlc.felear.xiaoensale.entity.ActionStepItem;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.entity.FlavourSpeed;
import com.dlc.felear.xiaoensale.entity.FoodCategoty;
import com.dlc.felear.xiaoensale.entity.LocalUser;
import com.dlc.felear.xiaoensale.entity.MenuFoodMaterial;
import com.dlc.felear.xiaoensale.entity.MenuRecordInfo;
import com.dlc.felear.xiaoensale.entity.MenuType;
import com.dlc.felear.xiaoensale.entity.Operator;
import com.dlc.felear.xiaoensale.entity.SfPosition;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

public class DetailDataOpenHelper extends OrmLiteSqliteOpenHelper {

    //定义数据库存放位置，便于以后查看
    public static final String DATABASE_PATH = Environment.getExternalStorageDirectory() + "/zkyc/zky.db";
    //定义数据库的版本号，当数据库需要升级时进行更改
    public static final int DATABASE_VERSION = 10;
    //创建DetailDataOpenHelper实例
    private static DetailDataOpenHelper instance;

    public DetailDataOpenHelper(Context context) {
        super(context, DATABASE_PATH, null, DATABASE_VERSION);
    }

    public static DetailDataOpenHelper getInstance(Context mContext) {
        if (instance == null) {
            synchronized (DetailDataOpenHelper.class) {
                if (instance == null) {
                    instance = new DetailDataOpenHelper(mContext);
                }
            }
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, MenuRecordInfo.class);
            TableUtils.createTable(connectionSource, MenuType.class);
            TableUtils.createTable(connectionSource, ActionStepItem.class);
            TableUtils.createTable(connectionSource, Operator.class);
            TableUtils.createTable(connectionSource, CookingMenu.class);
            TableUtils.createTable(connectionSource, MenuFoodMaterial.class);
            TableUtils.createTable(connectionSource, FlavourSpeed.class);
            TableUtils.createTable(connectionSource, LocalUser.class);
            TableUtils.createTable(connectionSource, SfPosition.class);
            TableUtils.createTable(connectionSource, FoodCategoty.class);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {

            TableUtils.createTable(connectionSource, MenuFoodMaterial.class);
            TableUtils.dropTable(connectionSource, MenuFoodMaterial.class, false);

            TableUtils.createTable(connectionSource, MenuRecordInfo.class);
            TableUtils.dropTable(connectionSource, MenuRecordInfo.class, false);

            TableUtils.createTable(connectionSource, MenuType.class);
            TableUtils.dropTable(connectionSource, MenuType.class, false);

            TableUtils.createTable(connectionSource, ActionStepItem.class);
            TableUtils.dropTable(connectionSource, ActionStepItem.class, false);

            TableUtils.createTable(connectionSource, CookingMenu.class);
            TableUtils.dropTable(connectionSource, CookingMenu.class, false);


            TableUtils.createTable(connectionSource, Operator.class);
            TableUtils.dropTable(connectionSource, Operator.class, false);

            TableUtils.createTable(connectionSource, FlavourSpeed.class);
            TableUtils.dropTable(connectionSource, FlavourSpeed.class, false);

            TableUtils.createTable(connectionSource, LocalUser.class);
            TableUtils.dropTable(connectionSource, LocalUser.class, false);

            TableUtils.createTable(connectionSource, SfPosition.class);
            TableUtils.dropTable(connectionSource, SfPosition.class, false);

            TableUtils.createTable(connectionSource, FoodCategoty.class);
            TableUtils.dropTable(connectionSource, FoodCategoty.class, false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

